package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.dao.IDestinationSearchDAO;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IDestinationSearchService;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.util.ESSearchUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.SearchQuery;

import java.util.ArrayList;
import java.util.List;

@Service
public class DestinationSearchServiceImpl implements IDestinationSearchService {

    @Autowired
    private IDestinationSearchDAO destinationSearchDAO;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private TransportClient client;





    @Override
    public void save(DestinationTemplate template) {
        destinationSearchDAO.save(template);
    }

    @Override
    public void update(DestinationTemplate template) {
        destinationSearchDAO.deleteById(template.getId());
        this.save(template);
    }

    @Override
    public void delete(Long id) {
        destinationSearchDAO.deleteById(id);
    }

    @Override
    public DestinationTemplate get(Long id) {
        return destinationSearchDAO.findById(id).get();
    }

    @Override
    public List<DestinationTemplate> list() {
        Iterable<DestinationTemplate> all = destinationSearchDAO.findAll();
        List<DestinationTemplate> list = new ArrayList<>();
        all.forEach(cc -> list.add(cc));
        return list;
    }

    @Override
    public Page<DestinationTemplate> query(SearchQuery qo) {
        Page<DestinationTemplate> page = destinationSearchDAO.findAll(qo.getPageable());


        return page;
    }

    @Override
    public DestinationTemplate getByName(String name) {
        Iterable<DestinationTemplate> iter = destinationSearchDAO.search(QueryBuilders.termQuery("name", name));
        List<DestinationTemplate>  list = new ArrayList<>();
        iter.forEach(a -> list.add(a));
        return list.size() >0? list.get(0):null;
    }

    public DestinationTemplate queryDestByName(String keyword) {
        Iterable<DestinationTemplate> dests  = destinationSearchDAO.search(QueryBuilders.termQuery("name", keyword));
        List<DestinationTemplate> list = new ArrayList<>();
        dests.forEach(dest->{
            list.add(dest);
        });
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }


    @Override
    public void createIndex() {
        elasticsearchTemplate.createIndex(DestinationTemplate.class);
        elasticsearchTemplate.putMapping(DestinationTemplate.class);
    }

    @Override
    public List<DestinationTemplate> fullSearch(SearchQueryObject qo) {
        return ESSearchUtil.fullSearch(client, DestinationTemplate.INDEX_NAME,
                DestinationTemplate.TYPE_NAME,new String[]{"destName", "info"}, qo, DestinationTemplate.class);
    }
}
